package org.jasper.plugin;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRRewindableDataSource;

import org.hibernate.ScrollableResults;

/**
 * Hibernate data source that uses <code>org.hibernate.Query.scroll()</code>.
 * 
 * @author PatelPr1
 */
public class JRHibernateScrollDataSource extends JRHibernateAbstractDataSource implements JRRewindableDataSource
{
	private ScrollableResults scrollableResults;

	public JRHibernateScrollDataSource(JRHibernateQueryExecuter queryExecuter, boolean useFieldDescription)
	{
		super(queryExecuter, useFieldDescription, true);

		scrollableResults = queryExecuter.scroll();
	}

	public boolean next() throws JRException
	{
		if (scrollableResults != null && scrollableResults.next())
		{
			setCurrentRowValue(scrollableResults.get());
			return true;
		}
		
		return false;
	}

	public void moveFirst()
	{
		queryExecuter.closeScrollableResults();
		scrollableResults = queryExecuter.scroll();
	}
}
